layui.use(['upload', 'element', 'layer', 'layedit', 'form'], function () {
    var $ = layui.jquery
        , upload = layui.upload
        , element = layui.element
        , layer = layui.layer
        , form = layui.form
        , layedit = layui.layedit;


    form.render('select');

    form.render("radio");

    //建立编辑器
    var build = layedit.build('productDesc');


    //常规使用 - 普通图片上传
    var uploadInst = upload.render({
        elem: '#test1'
        , url: 'http://localhost:8080/back/product/upload' //此处用的是第三方的 http 请求演示，实际使用时改成您自己的上传接口即可。
        ,filed:"upload"
            ,headers:{'username':localStorage.getItem("loginUser")}
        , before: function (obj) {
            //预读本地文件示例，不支持ie8
            obj.preview(function (index, file, result) {
                $('#demo1').attr('src', result); //图片链接（base64）
            });

        }
        , done: function (res) {
            //如果上传失败
            if (res.code != 200) {
                return layer.msg('上传失败');
            }
            sessionStorage.setItem("imgHref", res.data);
            //上传成功的一些操作
            //……
            $('#demoText').html(''); //置空上传失败的状态
        }
        , error: function () {
            //演示失败状态，并实现重传
            var demoText = $('#demoText');
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
            demoText.find('.demo-reload').on('click', function () {
                uploadInst.upload();
            });
        }
        //进度条
        , progress: function (n, elem, e) {
            element.progress('demo', n + '%'); //可配合 layui 进度条元素使用
            if (n == 100) {
                layer.msg('上传完毕', {icon: 1});
            }
        }
    });

    //监听修改提交
    //提交编辑内容
    form.on('submit(add)',
        function (data) {
            data.field.description = layedit.getContent(build);
            data.field.imgHref = sessionStorage.getItem("imgHref");
            data.field.enable = (data.field.enable == '启用') ? 1 : 0;
            console.log("提交的内容", data.field);

            $.ajax({
                url: "http://localhost:8080/back/front/insert",
                type: "post",
                headers:{'token':getCookie("keji_token"),'user':getCookie("keji_ucenter")},
                data:  JSON.stringify(data.field),
                contentType:"application/json",
                dataType: "json",
                success: function (res) {
                    if (res.msg == "success") {
                        //发异步，把数据提交给php
                        layer.alert("增加成功", {
                                icon: 6
                            },
                            function () {
                                //关闭当前frame
                                xadmin.close();

                                // 可以对父窗口进行刷新
                                xadmin.father_reload();
                            });
                    } else {
                        layer.alert("增加失败");
                    }
                }
            })


            return false;
        });

});
function setCookie(name,value)
{
    var Days = 30;
    var exp = new Date();
    exp.setTime(exp.getTime() + Days*24*60*60*1000);
    document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}
function clearCookie(name) {
    setCookie(name, "", -1);
}
function clearAllCookie() {
    var date=new Date();
    date.setTime(date.getTime()-10000);
    var keys=document.cookie.match(/[^ =;]+(?=\=)/g);
    console.log("需要删除的cookie名字："+keys);
    if (keys) {
        for (var i =  keys.length; i--;)
            document.cookie=keys[i]+"=0; expire="+date.toGMTString()+"; path=/";
    }
}
function getCookie(name)
{
    var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
    if(arr=document.cookie.match(reg))
        return unescape(arr[2]);
    else
        return null;
}
